//引入腾讯地图api
import QQMapWX from '@/utils/qqmap-wx-jssdk.min.js'
import api from '@/api/index'
const QQMapKey = 'FFEBZ-4NNCX-R6U4P-77O3L-QNEX6-CNB7X'
const qqmapsdk = new QQMapWX({
	key: QQMapKey
});
const txMapModule = {
	state:{
		//默认省市区街道等信息
		address_component:{},
		//默认详细地址
		address_info:"",
		//默认前端展示地址
		address:"正在获取位置中...",
		//经度
		latitude:40.825951,
		//纬度
		lontitude:114.889923,
		//社区ID
		scsId:-1
	},
	mutations:{
		setAddressComponent:(state,newAddressComponent)=>{
			state.address_component = newAddressComponent
		},
		setAddressInfo:(state,newAddressInfo)=>{
			state.address_info = newAddressInfo
		},
		setAddress:(state,newAddress)=>{
			state.address = newAddress
		},
		setLatitude:(state,newLatitude)=>{
			state.latitude = newLatitude
		},
		setLontitude:(state,newLontitude)=>{
			state.lontitude = newLontitude
		},
		setScsId:(state,scs_id)=>{
			state.scsId = scs_id
		}
	},
	getters:{
		getLatitude(state){
			return state.latitude;
		},
		getLontitude(state){
			return state.lontitude
		},
		getScsId(state){
			return state.scsId
		}
	},
	actions:{
		setAddressAct(context,data){
			context.commit('setAddress',data.name)
			context.commit('setLatitude',data.latitude)
			context.commit('setLontitude',data.longitude)
		},
		setBaseAddress(context,data){
			context.commit('setAddress',data.name)
			context.commit('setLatitude',data.latitude)
			context.commit('setLontitude',data.longitude)
			context.commit('setScsId',data.id)
		},
		getAddress(context){
			uni.authorize({
				scope: "scope.userLocation",
				success(){
					//同意授权
					uni.getLocation({
						type:'gcj02',
						success:function(res){
							let postData = {latitude:res.latitude,longitude:res.longitude}
							api.community.getCommunityList(postData)
							.then(res => {
								console.log('同意授权')
								console.log(res)
								if(res.code == 200){
									let data = res.data
									if(data.length > 0){
										context.commit('setAddress',data[0].name)
										context.commit('setLatitude',data[0].latitude)
										context.commit('setLontitude',data[0].longitude)
										context.commit('setScsId',data[0].id)
									}else{
										context.commit('setAddress','附近暂无可跑腿社区')
									}
								}else{
									uni.showToast({
										title:res.msg
									})
								}
							})
							.catch(err => {
								uni.showToast({
									title:err
								})
							})
						}
					})
				},
				fail(){
					//拒绝授权
					uni.showModal({
						title: '请授权您的地理位置',
						content: '该应用需要授权您的地理位置,否则应用将无法使用',
						success(res) {
							uni.openSetting({
								success(res) {
									if (res.authSetting["scope.userLocation"] == true) {
										uni.showToast({
											title:"授权成功",
											icon:"success",
											duration:1000
										})
									}else{
										uni.showToast({
											title:"授权失败,部分功能将无法使用",
											icon:"none",
											duration:2000
										})
										context.commit('setAddress','未授权位置信息,请点击此处...')
									}
								},
								fail() {
									context.commit('setAddress','未授权位置信息,请点击此处...')
								}
							})
						},
						fail(res) {
							uni.showToast({
								title:"您已拒绝授权,部分功能将无法使用",
								icon:"none",
								duration:2000
							})
							context.commit('setAddress','未授权位置信息,请点击此处...')
						}
					})
				}
			})
		},
		openMap(context,param){
			uni.getSetting({
				success: (res) => {
					if (res.authSetting['scope.userLocation']) { /* 用户授权成功时走这里 */
						uni.chooseLocation({
							latitude: param.latitude, /* 纬度 */
							longitude: param.longitude, /* 经度 */
							success: (res) => {
								qqmapsdk.reverseGeocoder({
									location: {
										latitude: res.latitude,
										longitude: res.longitude
									},
									success(res) {
										console.log(res)
										context.commit('setAddressComponent',res.result.address_component)
										context.commit('setAddressInfo',res.result.address)
										context.commit('setAddress',res.result.formatted_addresses.recommend)
										context.commit('setLatitude',res.result.location.lat)
										context.commit('setLontitude',res.result.location.lng)
									}
								})
							}
						})
					} else{
						uni.openSetting({
							success: (res) => {
							  if (res.authSetting["scope.userLocation"]) {
								uni.chooseLocation({
									latitude: param.latitude || '', /* 纬度 */
									longitude: param.longitude || '', /* 经度 */
									success: (res) => {
										qqmapsdk.reverseGeocoder({
											location: {
												latitude: res.latitude,
												longitude: res.longitude
											},
											success(res) {
												context.commit('setAddressComponent',res.result.address_component)
												context.commit('setAddressInfo',res.result.address)
												context.commit('setAddress',res.result.formatted_addresses.recommend)
												context.commit('setLatitude',res.result.location.lat)
												context.commit('setLontitude',res.result.location.lng)
											}
										})
									}
								})
							  }
							}
						})
					}
				}
			})
		}
	}
}
export default txMapModule